package com.mmall.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mmall.common.ExceptionError;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysRoleAclMapper;
import com.mmall.dao.SysRoleMapper;
import com.mmall.dao.SysRoleUserMapper;
import com.mmall.dao.SysUserMapper;
import com.mmall.model.SysRole;
import com.mmall.model.SysUser;
import com.mmall.param.RoleParam;
import com.mmall.util.BeanVaildator;
import com.mmall.util.IpUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleService {

    @Resource
    private SysRoleMapper mapper;
    @Resource
    private SysRoleUserMapper sysRoleUserMapper;
    @Resource
    private SysRoleAclMapper sysRoleAclMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysLogService sysLogService;

    public void save(RoleParam param){
        BeanVaildator.check(param);
        if (checkExist(param.getId(),param.getName())){
            ExceptionError.throwError("角色名称已经存在");
        }
        SysRole role = SysRole.builder().name(param.getName())
                .type(param.getType())
                .remark(param.getRemark())
                .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()))
                .operator(RequestHolder.getCurrentUser().getUsername())
                .operateTime(new Date())
                .status(param.getStatus()).build();
        mapper.insertSelective(role);
        sysLogService.saveRoleLog(null,role);
    }

    public void update(RoleParam param){
        BeanVaildator.check(param);
        if (checkExist(param.getId(),param.getName())){
            ExceptionError.throwError("角色名称已经存在");
        }

        SysRole before = mapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待修改的角色不存在");

        SysRole role = SysRole.builder().id(param.getId())
                .name(param.getName())
                .type(param.getType())
                .remark(param.getRemark())
                .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()))
                .operator(RequestHolder.getCurrentUser().getUsername())
                .operateTime(new Date())
                .status(param.getStatus()).build();
        mapper.updateByPrimaryKeySelective(role);
        sysLogService.saveRoleLog(before,role);
    }

    private boolean checkExist(Integer id, String name) {
        return mapper.countByName(id,name) > 0;
    }

    public List<SysRole> getAll() {
        return mapper.getAll();
    }

    public void delete(int roleId) {
        SysRole role = mapper.selectByPrimaryKey(roleId);
        Preconditions.checkNotNull(role,"待删除的角色不存在");
        mapper.deleteByPrimaryKey(roleId);
        sysLogService.saveRoleLog(role,null);
    }

    public List<SysRole> getRoleListByAclId(int aclId) {
        List<Integer> roleList = sysRoleAclMapper.getRoleListByAclId(aclId);
        if (CollectionUtils.isEmpty(roleList)){
            return Lists.newArrayList();
        }
        return mapper.getByIdList(roleList);
    }

    public List<SysUser> getUserListByRoleList(List<SysRole> roleList) {
        if (CollectionUtils.isEmpty(roleList)){
            return Lists.newArrayList();
        }
        List<Integer> roleIds = roleList.stream().map(r->r.getId()).collect(Collectors.toList());
        List<Integer> userList = sysRoleUserMapper.getUserIdListByRoleIdList(roleIds);
        if (CollectionUtils.isEmpty(userList)){
            return Lists.newArrayList();
        }
        return sysUserMapper.getByIdList(userList);
    }

    public List<SysRole> getRoleListByUserId(Integer userId) {
        List<Integer> roleIdList = sysRoleUserMapper.getRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        return mapper.getByIdList(roleIdList);
    }
}
